package com.cuit.xsc.weishare.activity;


import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.cuit.xsc.weishare.R;
import com.cuit.xsc.weishare.logic.MainService;
import com.cuit.xsc.weishare.model.UserInfo;
import com.cuit.xsc.weishare.services.UserInfoServices;
import com.cuit.xsc.weishare.util.ConfigUtil;
import com.cuit.xsc.weishare.util.IMessageDialogListener;
import com.cuit.xsc.weishare.util.LogUtil;
import com.cuit.xsc.weishare.util.MessageDialog;

public class LoginAct extends BaseActivity implements OnClickListener, IMessageDialogListener
{
	private ImageView qqImageView;
	private ImageView sinaImageView;
	private UserInfoServices uServices;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_);
		qqImageView = (ImageView) findViewById(R.id.login_qq);
		sinaImageView = (ImageView) findViewById(R.id.login_sina);
		
		qqImageView.setOnClickListener(this);
		sinaImageView.setOnClickListener(this);
		uServices = new UserInfoServices(this);
	}

	@Override
	public void onClick(View v)
	{
		Intent intent = new Intent(this, AuthAct.class);
		List<UserInfo> list = null;
		
		
		switch (v.getId())
		{
		case R.id.login_qq:
			list = findUserInfos(ConfigUtil.QQW);
			intent.putExtra(ConfigUtil.TYPE, ConfigUtil.QQW);
			break;
		case R.id.login_sina:
			list = findUserInfos(ConfigUtil.SINAW);
			intent.putExtra(ConfigUtil.TYPE, ConfigUtil.SINAW);
			break;
		}
		if (list != null)
		{
			LogUtil.i(this, "userList = "+list.size());
			intent.setClass(this, UserSelecteAct.class);
			startActivity(intent);
		}
		else
		{
			startActivity(intent);
			//finish();
		}
		
	}
	
	private List<UserInfo> findUserInfos(String userType)
	{
		return uServices.findUsersByType(userType);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			MessageDialog dialog = new MessageDialog(this);
			dialog.ShowConfirm(10, "注意事项", "你确定要离开？", this);
			return true;

		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onDialogClickOk(int requestCode)
	{
		switch (requestCode)
		{
		case 10:
			Intent service = new Intent(this, MainService.class);
			stopService(service);
			int nPid = android.os.Process.myPid();
			android.os.Process.killProcess(nPid);
			break;
		default:
			break;
		}
	}

	@Override
	public void onDialogClickCancel(int requestCode)
	{
		switch (requestCode)
		{
		case 10:
			
			break;

		default:
			break;
		}
		
	}

	@Override
	public void onDialogClickClose(int requestCode)
	{
		
	}

}
